package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.VerificationVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserService;
import org.apache.catalina.connector.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LiKe
 * @date 2022/1/21 19:47
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserService userService;
    /**
     * 根据id查询用户信息
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID){
        //调用ThreadLocal获取id
        Long id = UserHolder.getUserId();
        //如果请求中没有给id 就用Tonken中的
        if (userID == null){
            userID = id;
        }
        UserInfoVo userInfoVo = userService.fingById(userID);

        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户数据
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        //调用ThreadLocal获取id
        Long id = UserHolder.getUserId();
        //填充
        userInfo.setId(id);

        userService.updateUser(userInfo);
        return ResponseEntity.ok(null);

    }

    /**
     * 用户修改手机号发验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //1.获取当前用户手机号
        String mobile = UserHolder.getMobile();
        //2.调用service
        userService.sendCode(mobile);

        return ResponseEntity.ok(null);
    }

    /**
     * 验证验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        //1.获取用户输入的验证码
        String verificationCode = (String) map.get("verificationCode");
        //2.调用service
        boolean verification = userService.checkCode(verificationCode);

        VerificationVo vo = new VerificationVo();
        vo.setVerification(verification);

        return ResponseEntity.ok(vo);
    }

    /**
     * 修改手机号
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        //1.获取用户输入的验证码
        String phone = (String) map.get("phone");
        //2.调用service
        userService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map map = userService.counts();
        return ResponseEntity.ok(map);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity page(@PathVariable int type,
                               @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pagesize
                               ){
        PageResult pageResult = userService.page(type,page,pagesize,null);
        return ResponseEntity.ok(pageResult);
    }


}
